package com.g2room.driving.grap.life;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.ComCompany;
import com.g2room.driving.grap.life.model.po.ComInfo;
import com.g2room.driving.grap.life.model.po.ComType;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrabQyxyInfoDataByProxy {
	public String baseUrl = "http://www.qiyexinyong.org";
	private String source = "企业信用网";
	
	public static void main(String[] args) throws Exception{
		GrabQyxyInfoDataByProxy gkd = new GrabQyxyInfoDataByProxy();
		gkd.getList();
    }
	
	
	/**
	 * 获取头部导航菜单
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList() {
		String url = "http://www.qiyexinyong.org";
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(null == doc) return;
		Elements liels = doc.getElementsByAttributeValue("class", "newslist").first().getElementsByTag("li");
		Map<String, String> detailMap = new HashMap<String, String>();
		for(Element el : liels) {
			Element ael = el.getElementsByTag("a").first();
			String detailUrl = baseUrl + ael.attr("href");
			ael.remove();
			detailMap.put(detailUrl, el.text());
		}
		
		for(String detailUrl : detailMap.keySet()) {
			getDetail(detailUrl, detailMap.get(detailUrl));
		}
	}

	public void getDetail(String url, String type) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch(Exception e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("13");
			ge.setContent(type);
			ge.setDescription("获取Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		
		ComInfo ci = new ComInfo();
		ci.setType(type);
		ci.setTitle(doc.getElementsByAttributeValue("class", "sosuo_title").first().text());
		ci.setPublishTime(doc.getElementsByAttributeValue("class", "sosuo_date").first().text());
		ci.setContent(doc.getElementsByAttributeValue("class", "sosuo_content").first().html());
		ci.setSource(source);
		ci.setSourceUrl(url);
		
		
		insert2ComDb(ci);
	}
	
	public void insert2ComDb(ComInfo ci) {
		String sql = "insert into com_info(title,content,source,source_url,publish_time,type) values(?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getComConn().prepareStatement(sql);
			/**
title,content,source,source_url,publish_time,type
			 */
			ps.setString(1, ci.getTitle());
			ps.setString(2, ci.getContent());
			ps.setString(3, ci.getSource());
			ps.setString(4, ci.getSourceUrl());
			ps.setString(5, ci.getPublishTime());

			ps.setString(6, ci.getType());
			ps.execute();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(ci.getSourceUrl());
			ge.setType("14");
			ge.setContent(JSON.toJSONString(ci));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
